/**
 * 系统用户查看js
 */
var sysUserSelectOrg={
	dataCache:null,
	duties:null,
	selectOrgFullName:null,
	selectOrgId:null,

	getResource:function(){
		//获取所有职务
		var url = 'mgr/OrganizationCtrl/listAllDep';
		var params = null;

		var cb = function(data) {
			console.trace("data code = " & data.resultCode);
			if (data.resultCode == 200) {
				sysUserSelectOrg.duties = data.result;
				sysUserSelectOrg.showDuties();
			}
		};
		sendApiNoLoadSync(url, params, cb, 'GET');
	},
	showDuties:function(){
		var setting = {
			check : {
				enable : true,
				chkStyle : "radio",
				radioType : "all"
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			view : {
				nameIsHTML : true
			},
			callback : {
				onCheck : zTreeOnClick,
				beforeCheck: zTreeBeforeCheck
			}
		};
		var zNodes = [];
		var duties = sysUserSelectOrg.duties;

		var orgId=$('#com-addForm input[name="orgId"]').val();
		for (var i = 0; i < duties.length; i++) {
			zNodes[i] = {
				id : duties[i].id,
				name : duties[i].name,
				pId : duties[i].parentID,
				orgFullName: duties[i].flag02,
			};
			if (i == 0) {
				zNodes[i].open = true;
			}

			if(orgId == duties[i].id){
				zNodes[i].checked=true;
			};
		}
		$.fn.zTree.init($("#dutTree"), setting, zNodes);
	},

	init:function(){
		sysUserSelectOrg.getResource();
	}
}
$(document).ready(function() {
	sysUserSelectOrg.init();
});


function zTreeOnClick(event, treeId, treeNode) {
	$("#orgId").val(treeNode.id);
	sysUserSelectOrg.selectOrgFullName = treeNode.orgFullName;

};

function zTreeBeforeCheck(treeId, treeNode, clickFlag) {
	return true;//当是父节点 返回false 不让选取
};


function submitForm(){
	sysUserSelectOrg.selectOrgId = $("#orgId").val();

	var returnVale = false;
	if(sysUserSelectOrg.selectOrgId != null){
		localStorage.setItem("Selected_Org_FullName", sysUserSelectOrg.selectOrgFullName);
		localStorage.setItem("Selected_Org_Id", sysUserSelectOrg.selectOrgId);
		returnVale = true;
	}else{
		alert("请选择至少一个部门！");

	}

	return returnVale;
}
